package com.young.eventcoach.board;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.young.eventcoach.R;
import com.young.eventcoach.board.BoardListFooterView.OnMoreListShowListener;
import com.young.eventcoach.main.CommandBoard;
import com.young.eventcoach.main.MyDialogFragment;
import com.young.eventcoach.main.PropertyManager;
import com.young.eventcoach.network.BoardItemData;
import com.young.eventcoach.network.DownloadThread;
import com.young.eventcoach.network.NetworkRequest;
import com.young.eventcoach.upload.UploadResult;

public class BoardDetailHeaderView extends LinearLayout {
	ImageView userImageView;
	TextView boardTitleView;
	TextView boardWriter;
	ImageView boardImageView;
	TextView boardContentView;
	TextView boardReplyCnt;
	Button deleteButton;

	public interface OnDeleteClickListener {
		public void onDeleteClick();
	}
	
	private OnDeleteClickListener mListener;
	
	public void setOnDeleteClickListener(OnDeleteClickListener listener) {
		mListener = listener;
	}
	
	public BoardDetailHeaderView(Context context) {
		super(context);
	}

	public BoardDetailHeaderView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public BoardDetailHeaderView(Context context, BoardItemData mItem) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.board_show_header, this);

		userImageView = (ImageView) findViewById(R.id.userImage);
		boardTitleView = (TextView) findViewById(R.id.boardTitle);
		boardImageView = (ImageView) findViewById(R.id.boardImage);
		boardContentView = (TextView) findViewById(R.id.boardcontent);
		boardWriter = (TextView)findViewById(R.id.boardWriter);
		boardReplyCnt = (TextView)findViewById(R.id.boardReplyCnt);
		deleteButton = (Button)findViewById(R.id.delete);
		
		 if (mItem.userid == PropertyManager.getInstance().getUserDBId()) {
//		    	modifyButton.setVisibility(View.GONE);
		    	deleteButton.setVisibility(View.VISIBLE);
		    } else {
//		    	modifyButton.setVisibility(View.GONE);
		    	deleteButton.setVisibility(View.GONE);
		    }
		    
			deleteButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mListener.onDeleteClick();
				}
			});
		
		Log.d("BoardDetailHeaderView", "boardImageUrl: " + mItem.boardImageUrl.trim());
		Log.d("BoardDetailHeaderView", "boardImageUrl: " + (mItem.boardImageUrl.trim().equalsIgnoreCase(CommandBoard.NO_IMAGE_URL))); 
		if(mItem.boardImageUrl.trim().equals(CommandBoard.NO_IMAGE_URL)){
			boardImageView.setVisibility(View.GONE);
		}
		
		setImage(mItem.userImageUrl, userImageView);
		setImage(mItem.boardImageUrl, boardImageView);
		boardTitleView.setText(mItem.title);
		boardContentView.setText(mItem.content);
		boardWriter.setText("by "+mItem.userLoginId);
		boardReplyCnt.setText(mItem.replyCount+"");
		
	}
	
	
	public void setImage(String imageUrl,final ImageView imageView) {
		if (imageUrl != null && !imageUrl.equals("")) {
			ImageRequest request = new ImageRequest(imageUrl+CommandBoard.SIZE_PARAM_LARGE);
			request.setOnDownloadCompletedListener(new NetworkRequest.OnDownloadCompletedListener() {
				
				@Override
				public void onDownloadCompleted(int result, NetworkRequest request) {
					// TODO Auto-generated method stub
					if (result == NetworkRequest.PROCESS_SUCCESS) {
						ImageRequest ir = (ImageRequest)request;
						Bitmap bm = ir.getBitmap();
						if (bm != null) {
							imageView.setImageBitmap(bm);
						}
					}
				}
			});
			ImageManager.getInstance().enqueue(request);
		}
	}

}
